using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Wumpus
{
    public class Images
    {
        public Texture2D Planet;
        public Texture2D Wumpus;
        public Texture2D Line;
    }

    public class ViewportScale
    {
        public Vector2 MaxSize;
        public Vector2 CurrentStart;
        public Vector2 CurrentSize;
        public float Rotation;
    }

    public class GraphicsEngine
    {
        //QUESTION: why would you make this protected?
        protected GraphicsDevice GraphicsDevice;
        protected Images Images;
        protected SpriteBatch SpriteBatch;
        protected List<Vector2> PlanetCoords;
        protected GameState GameState;

        public GraphicsEngine(GameState state, GraphicsDevice GraphicsDevice, Images Images)
        {
            this.GraphicsDevice = GraphicsDevice;
            this.Images = Images;
            this.SpriteBatch = new SpriteBatch(GraphicsDevice);
            this.PlanetCoords = new List<Vector2>();
        }

        public void Render(GameState Gamestate)
        {
            this.DrawBackground();

            this.SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            // NOTE: can merge the two functions below into 'for' loop for optimization
            this.DrawPaths(GameState.Connections);
            this.DrawPlanets(PlanetCoords);
            this.SpriteBatch.End();
        }

        protected void DrawBackground()
        {
            this.GraphicsDevice.Clear(Color.White);
        }

        protected void DrawPlanets(List<Vector2> PlanetCoords)
        {
            foreach (Vector2 coord in PlanetCoords)
            {
                this.SpriteBatch.Draw(this.Images.Planet, coord, Color.White);
            }
        }

        protected void DrawPaths(Connections Connections)
        {
            foreach (List<int> planetConnections in Connections)
            {
                foreach (int connection in planetConnections)
                {
                    //TODO: draw line between planets if it hasn't already been done
                }
            }
        }

        protected void DrawSinglePath(Vector2 p1, Vector2 p2, float width)
        {
            float angle = (float)Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
            float length = Vector2.Distance(p1, p2) / 3; // Divide by 3 since the resource is 3x3 pixels.
                                                        // TODO: remove this magic number.

            this.SpriteBatch.Draw(
                this.Images.Line,
                p1,
                null,
                Color.Blue,
                angle,
                Vector2.Zero,
                new Vector2(length, width),
                SpriteEffects.None,
                0
            );
        }
    }

}

